package de.tu_darmstadt.gdi1.xcom.tests.testcases.students;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel4;

/**
 * 
 * Tests for actions performed by humans automatically after KI-actions
 * 
 * @author Lukas Schwoebel
 *
 */
public class AutoActionTest {
	
	private XcomTestAdapterLevel4 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel4();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel4#performActionForPlayer()}.
	 */
	@Test
	public final void testAutoActions() {

		testAdapter.loadLevelFromString(TestLevel.testLevel.replaceAll("@;4;2;n;50;75;50;50;10", "@;4;2;n;50;75;50;50;3"));
	
		// move zombie
		testAdapter.performKIAction("3;2;f");	
		
		// save gamestate
		String gameState = testAdapter.returnGamestateAsSavegame();
		
		// test for all players if they perform any auto-action
		testAdapter.performActionForPlayer(4, 2);
		testAdapter.performActionForPlayer(2, 5);
		testAdapter.performActionForPlayer(4, 6);
		assertEquals("No zombie in line of sight of any player, so the gamestate should not have changed!", gameState, testAdapter.returnGamestateAsSavegame());
		
		// now walk into line of sight with zombie
		testAdapter.performKIAction("3;1;r");
		testAdapter.performKIAction("3;1;f");		
		// perform auto-action, should kill the zombie
		testAdapter.performActionForPlayer(4, 2);
		
		// re-save gamestate
		gameState = testAdapter.returnGamestateAsSavegame();
		
		// test if zombie was killed
		assertTrue("Zombie should have been killed, munition and actionpoints of player at 4:2 should have decreased." + gameState, gameState.contains("@;4;2;n;50;65;50;50;2"));
		assertEquals("Zombie should have been killed at 4:1, a floor element should remain", '.', testAdapter.elementOnPosition(4, 1));
	}
	

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel4#performActionForPlayer()}.
	 */
	@Test
	public final void testAutoActionsNoAmmo() {

		testAdapter.loadLevelFromString(TestLevel.testLevel.replaceAll("@;4;2;n;50;75;50;50;10", "@;4;2;w;50;75;50;50;1"));
	
		// perform KI-action in order to end human round
		testAdapter.performKIAction("3;2;l");
		
		
		// test for player if he is killing
		testAdapter.performActionForPlayer(4, 2);
		// save gamestate
		String gameState = testAdapter.returnGamestateAsSavegame();
		assertTrue("Zombie should have been killed, munition and actionpoints of player at 4:2 should have decreased." + gameState, gameState.contains("@;4;2;w;50;65;50;50;0"));
		assertEquals("Zombie should have been killed at 4:1, a floor element should remain", '.', testAdapter.elementOnPosition(3, 2));
		
		// now test if player still does a auto-action, he shouldn't
		testAdapter.performActionForPlayer(4, 2);
		assertTrue("Zombie should not have been killed as there is no ammo left" + gameState, gameState.contains("@;4;2;w;50;65;50;50;0"));
		assertEquals("Zombie should not have been killed, still Z-element should remain", 'Z', testAdapter.elementOnPosition(2, 2));
			
		// end zombie-turn and start it again
		testAdapter.endTurn();
		testAdapter.performKIAction("2;2;l");
		
		// re-save gamestate
		gameState = testAdapter.returnGamestateAsSavegame();
		
		// now test if player still does a auto-action, he shouldn't again, as the ammo did not increased
		testAdapter.performActionForPlayer(4, 2);
		assertTrue("Zombie should not have been killed as there is still no ammo left" + gameState, gameState.contains("@;4;2;w;50;75;50;50;0"));
		assertEquals("Zombie should not have been killed, still Z-element should remain", 'Z', testAdapter.elementOnPosition(2, 2));
		
		// walk with zombie towards human on neighbor field, should still not attack
		testAdapter.performKIAction("2;2;l");
		testAdapter.performKIAction("2;2;l");
		testAdapter.performKIAction("2;2;f");
		testAdapter.performActionForPlayer(4, 2);
		assertTrue("Zombie should not have been killed as there is no ammo left and close combat is not allowed" + gameState, gameState.contains("@;4;2;w;50;75;50;50;0"));
		assertEquals("Zombie should not have been killed, still Z-element should remain", 'Z', testAdapter.elementOnPosition(3, 2));
		
		
		// now test action-points in the same manner
		gameState = testAdapter.returnGamestateAsSavegame().replaceAll("@;4;2;w;50;75;50;50;0", "@;4;2;w;50;0;50;50;1");
		testAdapter.loadLevelFromString(gameState);
		
		// ammo is there but no action-points --> no attack
		testAdapter.performActionForPlayer(4, 2);
		assertTrue("Zombie should not have been killed as there are no actionpoints left" + gameState, gameState.contains("@;4;2;w;50;0;50;50;1"));
		assertEquals("Zombie should not have been killed, still Z-element should remain", 'Z', testAdapter.elementOnPosition(3, 2));
		
	}
}
